package com.looookme.shaozhuo.msg;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.looookme.shaozhuo.mb_study.R;

/**
 * Created by Shaozhuo on 2015/9/8.
 */
public class Msg_AlertDialog extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        LinearLayout linearLayout = new LinearLayout(Msg_AlertDialog.this);
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout);

        Button button = new Button(this);
        button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        button.setText("AlertDialog");
        linearLayout.addView(button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog alert = new AlertDialog.Builder(Msg_AlertDialog.this).create();
                alert.setIcon(R.drawable.item_icon);
                alert.setTitle("系统提示");
                alert.setMessage("带取消、中立和确定按钮的对话框");
                alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Msg_AlertDialog.this, "确定按钮被点击", Toast.LENGTH_SHORT).show();
                    }
                });
                alert.setButton(DialogInterface.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Msg_AlertDialog.this, "中立按钮被点击", Toast.LENGTH_SHORT).show();
                    }
                });
                alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Msg_AlertDialog.this, "取消按钮被点击", Toast.LENGTH_SHORT).show();
                    }
                });
                alert.show();
            }
        });

        Button btnList = new Button(this);
        btnList.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        btnList.setText("AlertDialog List");
        linearLayout.addView(btnList);
        btnList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] items = new String[]{"跑步", "羽毛球", "乒乓球", "网球", "体操"};
                AlertDialog.Builder builder = new AlertDialog.Builder(Msg_AlertDialog.this);
                builder.setIcon(R.drawable.item_icon);
                builder.setTitle("请选择您喜欢的体育项目");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Msg_AlertDialog.this, "您选择了 " + items[i], Toast.LENGTH_SHORT).show();
                    }
                });
                builder.create().show();
            }
        });

        Button btnSingleList = new Button(this);
        btnSingleList.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        btnSingleList.setText("AlertDialog Single List");
        linearLayout.addView(btnSingleList);
        btnSingleList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] items = new String[]{"标准", "无声", "会议", "户外", "离线"};
                AlertDialog.Builder builder = new AlertDialog.Builder(Msg_AlertDialog.this);
                builder.setIcon(R.drawable.item_icon);
                builder.setTitle("请选择您使用的情景模式");
                builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Msg_AlertDialog.this, "您选择了 " + items[i], Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setPositiveButton("确定", null);
                builder.create().show();
            }
        });

        Button btnCheckList = new Button(this);
        btnCheckList.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        btnCheckList.setText("AlertDialog Checkbox List");
        linearLayout.addView(btnCheckList);
        btnCheckList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] items = new String[]{"植物大战僵尸", "风怒的小鸟", "表表龙", "开心龙城", "超级玛丽"};
                final boolean[] checks = new boolean[]{false, true, false, false, false};

                AlertDialog.Builder builder = new AlertDialog.Builder(Msg_AlertDialog.this);
                builder.setIcon(R.drawable.item_icon);
                builder.setTitle("请选择您喜欢的游戏");
                builder.setMultiChoiceItems(items, checks, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        checks[i] = b;
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int ii) {
                        String result = "";
                        for(int i = 0; i < checks.length; i++){
                            if(checks[i]){
                                result += items[i] + "、";
                            }
                        }

                        if(!"".equals(result)){
                            result = result.substring(0, result.length() - 1);
                            Toast.makeText(Msg_AlertDialog.this, "您选择了 " + result, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                builder.create().show();
            }
        });
    }
}
